﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SportStore.Models.Abstract;
using SportStore.Models.Entities;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.IO;

namespace SportStore.Models.Concrete
{

    public class EmailSettings
    {
        public string MailToAddress = "ua.xwrs@gmail.com";
        public string MailFromAddress = "ua.xwrs@gmail.com";
        public bool UseSsl = false;
        public string Username = "ua.xwrs";
        public string Password = "zombieinyourhead3323578813137c6";
        public string ServerName = "smtp.gmail.com";
        public int ServerPort = 578;
        public bool WriteAsFile = true;
        public string FileLocation = @"d:\";
    }

    public class EmailOrderProcessor:IOrderProcessor
    {
        private EmailSettings emailSettings;

        public EmailOrderProcessor()
        {
            emailSettings = new EmailSettings();
        }

        public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (var smtpClient = new SmtpClient())
            {
                smtpClient.EnableSsl = emailSettings.UseSsl;
                smtpClient.Host = emailSettings.ServerName;
                smtpClient.Port = emailSettings.ServerPort;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password);
                if (emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = emailSettings.FileLocation;
                    smtpClient.EnableSsl = false;
                }

                StringBuilder body = new StringBuilder().AppendLine("A new order has been submitted").AppendLine("---").AppendLine("Items");

                foreach (var line in cart.Lines)
                {
                    var subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotal: {2:c}", line.Quantity, line.Product.Name, subtotal);
                    body.AppendLine("");
                }

                body.AppendFormat("Total order value: {0:c}", cart.ComputeTotalValue())
                    .AppendLine("---")
                    .AppendLine("Ship to")
                    .AppendLine(shippingInfo.Name)
                    .AppendLine(shippingInfo.Line1)
                    .AppendLine(shippingInfo.Line2 ?? "")
                    .AppendLine(shippingInfo.Line3 ?? "")
                    .AppendLine(shippingInfo.City)
                    .AppendLine(shippingInfo.State ?? "")
                    .AppendLine(shippingInfo.Country)
                    .AppendLine(shippingInfo.Zip)
                    .AppendLine("---")
                    .AppendFormat("Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No");

                //MailMessage mailMessage = new MailMessage(emailSettings.MailFromAddress, emailSettings.MailToAddress, "New order submitted", body.ToString());
                //if (emailSettings.WriteAsFile)
                //{
                //    mailMessage.BodyEncoding = Encoding.ASCII;
                //}

                //smtpClient.Send(mailMessage);
                FileStream fs = new FileStream(@"d:\order.txt", FileMode.Create);
                fs.Write(Encoding.ASCII.GetBytes(body.ToString()), 0, body.Length);
                fs.Flush();
                fs.Close();
            }
        }
    }
}